﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web.Security;
using Press.MVC.Data.DataAccess.SqlServer;

namespace Press.MVC.Data
{
    public class SqlUserRepository: IUserRepository{

        LinqBlogDataContext _db;

        public SqlUserRepository(LinqBlogDataContext db)
        {
            _db = db;
        }


        public IQueryable<AspNetUser> GetAspNetUsers()
        {
            var result = from au in _db.aspnet_Users
                         select new AspNetUser
                         {
                             UserId = au.UserId
                         };
            return result;
        }

        /// <summary>
        /// Gets the BlogUserMap relationships
        /// </summary>
        /// <returns></returns>
        public IQueryable<BlogUserMap> GetBlogUserMap()
        {
            return from bu in _db.BlogUserAdmins
                   select new BlogUserMap
                   {
                       BlogId = bu.BlodId,
                       UserId = bu.UserId
                   };
        }


        //TODO get rid of everything below. 
        /// <summary>
        /// Returns a queryable list of users
        /// </summary>
        public IQueryable<User> GetUsers()
        {

            return from u in _db.Users
                   select new User
                   {
                       UserName = u.UserName,
                       Email = u.Email,
                       FirstName = u.FirstName,
                       LastName = u.LastName
                   };
        }

        ///// <summary>
        ///// Logs the user in with valid credentials
        ///// </summary>
        //public bool LoginUser(string userName, string password) {

        //    return Membership.ValidateUser(userName, password);


        //}

        ///// <summary>
        ///// Removes a user from the DB and Membership system
        ///// </summary>
        ///// <param name="userName"></param>
        ///// <returns></returns>
        //public bool DeleteUser(string userName) {
        //    bool result = false;
        //    try {
        //        using (Press.MVC.Data.DataAccess.SqlServer.LinqBlogDataContext db = new Press.MVC.Data.DataAccess.SqlServer.LinqBlogDataContext())
        //        {
        //            db.Users.DeleteAllOnSubmit(from u in db.Users
        //                                       where
        //                                           u.UserName == userName
        //                                       select u);
        //            db.SubmitChanges();
        //        }
               
        //        Membership.DeleteUser(userName, true);
                
        //        result = true;
        //    } catch {
        //        throw;
        //    }
        //    return result;
        //}

        ///// <summary>
        ///// Registers the user in the system
        ///// </summary>
        //public bool RegisterUser(string first, string last, string email, 
        //    string userName, string password, string question, string answer) {

        //    bool result = false;

        //    MembershipCreateStatus status;

        //    Membership.CreateUser(userName,
        //      password,
        //      email,
        //      question,
        //      answer, true, out status);

        //    if (status == MembershipCreateStatus.Success) {

        //        //add the user to the DB
        //        Press.MVC.Data.DataAccess.SqlServer.User newUser = new Press.MVC.Data.DataAccess.SqlServer.User();
        //        newUser.FirstName = first;
        //        newUser.LastName = last;
        //        newUser.Email = email;
        //        newUser.UserName = userName;
        //        newUser.CreatedOn = DateTime.Now;
        //        newUser.ModifiedOn = DateTime.Now;

        //        try {
        //            using (LinqBlogDataContext db = new LinqBlogDataContext())
        //            {
        //                db.Users.InsertOnSubmit(newUser);
        //                db.SubmitChanges();
        //            }
        //        } catch {
        //            //if it fails, pull the user out of the Membership system
        //            DeleteUser(userName);
        //            throw;
        //        }
        //        result = true;


        //    } else {

        //        if (status == MembershipCreateStatus.DuplicateEmail) {
        //            throw new InvalidOperationException("This email is already in our system");

        //        }
        //        if (status == MembershipCreateStatus.DuplicateUserName) {
        //            throw new InvalidOperationException("Need to use another login - this one's taken");

        //        }
        //        if (status == MembershipCreateStatus.InvalidEmail) {
        //           throw new InvalidOperationException("Invalid email address");

        //        }
        //        if (status == MembershipCreateStatus.InvalidPassword) {
        //            throw new InvalidOperationException("Invalid password. Needs to be stronger");

        //        }
        //        if (status == MembershipCreateStatus.UserRejected) {
        //            throw new InvalidOperationException("Cannot register at this time");

        //        }
        //    }
        //    return result;
        //}

    }
}
